﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Controls.Primitives;

namespace Nizm0.QOT.Library.Controls
{
    /// <summary>
    /// Interaction logic for ObejctList.xaml
    /// </summary>
    public partial class ObejctList : System.Windows.Controls.ListBox
    {
        public List<Objects.IProcedureInfo> _proclist;

        public ObejctList()
        {
        }

        public ObejctList(List<Objects.IProcedureInfo> procedurelist)
        {
            InitializeComponent();
            _proclist = procedurelist;
            
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, PasteCommandHandler, CanPasteExecuteHandler));

            //listbox1.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending));

            //foreach (Objects.ProcedureInfo tmpproc in _proclist)
            //    listbox1.Items.Add(new ProcedureListObject(tmpproc));

            //this.SelectionChanged += new SelectionChangedEventHandler(listbox1_SelectionChanged);
        }

        //public delegate void SelectionChangedEventHandler(object sender, SelectionChangedEventArgs e);
        //public event SelectionChangedEventHandler SelectionChanged;


        internal void Fill()
        {
            this.Items.Clear();
            foreach (Objects.ProcedureInfo tmpproc in _proclist)
            {
                if (this.FindName(tmpproc.Name) == null)
                {
                    ProcedureListObject proc = new ProcedureListObject(tmpproc);
                    //proc.Width = this.ActualWidth;
                    //this.Items.Add(proc);
                    this.Items.Add(proc);
                }
            }
            this.Items.SortDescriptions.Clear();

            SortDescription sd = new SortDescription("Name", ListSortDirection.Ascending);

            this.Items.SortDescriptions.Add(sd);
        }

        public bool Add(Library.Objects.IProcedureInfo procedure)
        {
            if (this.FindName(procedure.Name) == null)
            {
                ProcedureListObject proc = new ProcedureListObject((Library.Objects.ProcedureInfo)procedure);
                Library.Objects.ProjectSettings.Instance.AddProcedure((Library.Objects.ProcedureInfo)procedure);
                //proc.Width = this.ActualWidth;
                //this.Items.Add(proc);
                this.Items.Add(proc);
                return true;
            }
            else
                return false;
        }

        public bool Remove(Library.Objects.IProcedureInfo procedure)
        {
            if (this.FindName(procedure.Name) != null)
            {
                this.Items.Remove(this.FindName(procedure.Name));
                Library.Objects.ProjectSettings.Instance.RemoweProcedure(procedure);
                return true;
            }
            else
                return false;
        }

        public void HideCheckBox()
        {
            foreach (var item in this.Items)
            {
                ((ProcedureListObject)item).HideCheckBox();
            }
        }

        public void ShowCheckBox()
        {
            foreach (var item in this.Items)
            {
                ((ProcedureListObject)item).ShowCheckBox();
            }
        }


        private void CanCutExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            if (this.SelectedItems.Count >= 1)
                e.CanExecute = true;
        }

        private void CutCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void CanCopyExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            if (this.SelectedItems.Count >= 1)
                e.CanExecute = true;
        }

        private void CopyCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            List<Objects.ProcedureInfo> listproc = new List<Objects.ProcedureInfo>();
            foreach (var tmp in this.SelectedItems)
                listproc.Add(((ProcedureListObject)tmp).Procedure);
            Settings.Instance.ProjectSettings.CopyProcedures(listproc);
        }

        private void CanOpenResultExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {//((ProcedureListObject)this.SelectedItem).Procedure.ResultFile
            if (sender != null && this.SelectedItems.Count >= 1)
                ((ProcedureListObject)this.SelectedItem).CanOpenResultExecuteHandler(sender, e);
        }

        private void OpenResultCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            ((ProcedureListObject)this.SelectedItem).OpenResultCommandHandler(sender, e);
        }

        private void CanOpenFileLocationExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            if (sender != null && this.SelectedItems.Count >= 1)
                ((ProcedureListObject)this.SelectedItem).CanOpenFileLocationExecuteHandler(sender, e);
        }

        private void OpenFileLocationCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            ((ProcedureListObject)this.SelectedItem).OpenFileLocationCommandHandler(sender, e);
        }

        private void ListBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            this.ContextMenu = context;
        }

        private void CanPasteExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            if (sender != null && (System.Windows.Clipboard.ContainsText() || System.Windows.Clipboard.ContainsData("List<Objects.ProcedureInfo>")))
                e.CanExecute = true;
        }

        private void PasteCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            Settings.Instance.ProjectSettings.AddProcedureFromClipboard();
        }

        private void someListBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.UnselectAll();
            if (e.RightButton == MouseButtonState.Pressed)
            {
                //this.ContextMenu.
            }
        }

        private void CanDeleteExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            if (this.SelectedItems.Count >= 1)
                e.CanExecute = true;
        }

        private void DeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.SelectedIndex == -1)
            {
                return;
            }
            if (this.SelectedItems.Count >= 1)
            {
                var items = this.SelectedItems;
                while (items.Count>0)
                {
                    this.Items.Remove(items[0]);
                    //Remove(((ProcedureListObject)item).Procedure);
                }
            }
            else
                this.Items.RemoveAt(this.SelectedIndex);
        }

        private void RefreshCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.SelectedIndex == -1)
            {
                return;
            }
            if (this.SelectedItems.Count >= 1)
                foreach (var item in this.SelectedItems)
                {
                    Library.Objects.ProjectSettings.Instance.GetDataAboutProcedure(((ProcedureListObject)item).Procedure);
                }
        }

        private void CanRefreshExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        //private void listBox1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Right)
        //    {
        //        //select the item under the mouse pointer
        //        this.SelectedIndex = this.IndexFromPoint(e.Location);
        //        if (this.SelectedIndex != -1)
        //        {
        //            ContextMenu.Visibility = System.Windows.Visibility.Visible;
        //        }
        //    }
        //}

        //internal void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (SelectionChanged != null)
        //        SelectionChanged(sender, e);
        //}
    }
}
